<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>tag标签管理</span>
<!--        <el-button style="margin-left: 20px" size="small" type="primary">全选</el-button>-->
<!--        <el-button style="float: right; " size="small" type="success">添加</el-button>-->
<!--        <el-button style="float: right; " size="small" type="danger">删除</el-button>-->
      </div>
      <div>
        <el-tag
          style="margin: 10px"
          :key="tag.Id"
          v-for="tag in list"
          closable
          :disable-transitions="false"
          @close="handleClose(tag)">
          {{ tag.Title }}
        </el-tag>
        <el-input
          class="input-new-tag"
          v-if="inputVisible"
          v-model="inputValue"
          ref="saveTagInput"
          size="small"
          @keyup.enter.native="handleInputConfirm"
          @blur="handleInputConfirm"
        >
        </el-input>
        <el-button v-else class="button-new-tag" size="small" @click="showInput">+ New Tag</el-button>
      </div>
    </el-card>

  </div>
</template>

<script>
import {getTagList, delTag, addTag} from '@/api/tag'

export default {
  data() {
    return {
      list: [],
      search: '',
      dynamicTags: ['标签一', '标签二', '标签三'],
      inputVisible: false,
      inputValue: ''
    }
  },
  created() {
    this.getTag()
  },
  methods: {
    getTag() {
      getTagList().then(con => {
        console.log(con)
        this.list = con.data
      })
    },
    handleClose(tag) {
      console.log(tag)
      delTag(tag.Id).then(con => {

        this.getTag()
      })
    },

    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    handleInputConfirm() {
      const inputValue = this.inputValue;
      if (inputValue) {
        addTag(inputValue).then(con => {
          this.getTag()
        })
      }
      this.inputVisible = false;
      this.inputValue = '';
    }
  }
}
</script>
